import { defineStore } from 'pinia'
import { getDistanceApi } from '../api'

export const useAddress = defineStore('address', {
    state: () => {
        return {
            id: '',
            name: '',
            phone: '',
            address: '',
            location: '',
            defaultAddress: '',
            fare: 0,
            distance: 0,
            techLocation: ''
        }
    },
    actions: {
        async setAddress(state: { id: string; name: string; phone: string; address: string; location: string; defaultAddress: string; }) {
            this.id = state.id
            this.name = state.name
            this.phone = state.phone
            this.address = state.address
            this.location = state.location
            this.defaultAddress = state.defaultAddress
            const getDistance = await getDistanceApi({ from: this.techLocation, to: state.location })
            const distance = Number((getDistance.data / 2 / 500).toFixed(2))
            this.distance = distance
            this.fare = distance - 3 <= 0 ? 10 * 2 : ((distance - 3) * 2.5 + 10) * 2
        }
    }
})